package com.ytd.smartcanteen.weighting.receiver;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;

import com.ytd.common.util.CommandExecution;
import com.ytd.common.util.DownloadManagerUtil;
import com.ytd.common.util.LogUtil;
import com.ytd.common.util.ThreadUtil;
import com.ytd.common.util.YtdConfig;

import java.util.Objects;

import cn.ytd.smartcanteen.weighting.BuildConfig;

public class DownloadReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (Objects.requireNonNull(intent.getAction())) {
            case DownloadManager.ACTION_DOWNLOAD_COMPLETE:
                long myDownloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                LogUtil.e("接收到下载完成广播");
                if (YtdConfig.getModel() != null) {
                    if (myDownloadID == YtdConfig.getModel().getId()) {
                        DownloadManager downloadManager =
                                (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                        Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(myDownloadID));
                        if (cursor != null && cursor.moveToFirst()) {
                            @SuppressLint("Range") int status =
                                cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                            switch (status) {
                                case DownloadManager.STATUS_FAILED:
                                    LogUtil.e("下载失败");
                                    break;
                                case DownloadManager.STATUS_PAUSED:
                                    LogUtil.e("下载暂停");
                                    break;
                                case DownloadManager.STATUS_PENDING:
                                    LogUtil.e("下载状态不确定");
                                    break;
                                case DownloadManager.STATUS_RUNNING:
                                    LogUtil.e("下载中");
                                    break;
                                case DownloadManager.STATUS_SUCCESSFUL:
                                    LogUtil.e("下载成功");
                                    boolean b = DownloadManagerUtil.checkFile();
                                    if (b) {
                                        ThreadUtil.execute(() -> {
                                            LogUtil.e("下载成功---开始安装");
                                            CommandExecution.execCommand(
                                                "pm install -i " + BuildConfig.APPLICATION_ID + " -r -d " +
                                                    YtdConfig.getModel().getPath(), false);
                                        });
                                    } else {
                                        LogUtil.e("文件大小不匹配---重新下载");
                                        DownloadManagerUtil.reDownload();
                                    }
                                    break;
                            }

                        }
                    }
                }


                break;
            case DownloadManager.ACTION_NOTIFICATION_CLICKED:

                break;
        }
    }
}
